package com.itsm.example.audit.service.impl;


import com.itsm.example.audit.pojo.context.AuditContext;
import com.itsm.example.audit.pojo.domain.AuditDO;
import com.itsm.example.audit.pojo.enums.StateMachineEnum;
import com.itsm.example.audit.pojo.event.AuditEvent;
import com.itsm.example.audit.pojo.state.AuditState;
import com.itsm.example.audit.service.AuditService;
import com.itsm.example.statemachine.bo.ItsmContext;
import com.itsm.example.statemachine.bo.ItsmEvent;
import com.itsm.example.statemachine.bo.ItsmState;
import com.itsm.statemachine.Action;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 已完成执行动作
 *
 * @author benym
 * @date 2024/7/19 11:40
 */
@Component
@Slf4j
public class DoneAction implements Action<ItsmState, ItsmEvent, ItsmContext> {


    @Autowired
    private AuditService auditService;

    @Override
    public void execute(ItsmState from, ItsmState to, ItsmEvent event, ItsmContext context) {
        log.info("doneAction from {}, to {}, on event {}, id:{}", from, to, event, context.getId());
//        auditService.saveOrUpdate(new AuditDO(context.getId(), to.getValue(), context.getInstId()));
        AuditContext auditContext = (AuditContext) context;
        AuditDO auditDO = new AuditDO(context.getId(), to.getValue(), context.getInstId());
        auditService.saveOrUpdate(auditDO);
        context.setId(auditDO.getId());
        auditContext.setAuditDO(auditDO);
    }
}
